home *** CD-ROM | disk | FTP | other *** search
- Path: news.gwdg.de!amuelle3
- From: amuelle3@gwdu19.gwdg.de (Arne Mueller )
- Newsgroups: comp.lang.c
- Subject: how to prevent overflow in unsigned?
- Date: 16 Apr 1996 16:35:55 GMT
- Organization: GWDG, Goettingen
- Message-ID: <4l0i9b$efq@gwdu19.gwdg.de>
- NNTP-Posting-Host: gwdu08-fddi.gwdg.de
- X-Newsreader: TIN [version 1.1 PL8]
-
- Hello,
-
- I've the following problem:
-
- I have to write a function (I'm not allowed to use the standard functions of stdlib!)
- that reads an unsigned int from stdin (using getchar).
-
-
-
- My question: How to recognize an overflowWh
- If the number is bigger than UINT_MAX, the variable that saves the number
- is not set to zero.
-
- some code ... :
-
- IO_STATUS unsigned_read(unsigned int *i)
- {
-
- int c;
- unsigned int x;
- unsigned int i_old;
- IO_STATUS stat = IN_NOK;
-
- i_alt = *i = 0;
- while (isdigit((c = getchar())) != 0)
- {
- x = (unsigned int) c - 48; /* 48 is ASCII 0 */
- *i = *i * 10 + x;
-
- if (*i <= i_old)
- {
- puts("too big!");
- return IN_NOK;
- }
- i_old = *i;
- stat = IN_OK;
- }
-
- return OK;
- }
-
-
-